package ie.dcu.spca.view.bill;

import java.util.List;
import java.util.Map;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import ie.dcu.spca.Main;
import ie.dcu.spca.R;
import ie.dcu.spca.data.BasicDataManager;
import ie.dcu.spca.data.BillDataManager;
import ie.dcu.spca.data.ItemDataManager;
import ie.dcu.spca.data.UserDataManager;
import ie.dcu.spca.model.bean.Bill;
import ie.dcu.spca.model.bean.Item;
import ie.dcu.spca.view.CRUDDialog;
import ie.dcu.spca.view.DataList;
import ie.dcu.spca.view.product.ProductSearchActivity;

public class BillCRUDDialog extends CRUDDialog {

    private static BillDataManager billManager;
    private static UserDataManager userManager;
    private static ItemDataManager itemManager;

    private Bill bill;
    private String location;
    private int payerId;

    private EditText etLocation;
    private Spinner spPayerId;
    private LinearLayout billItemList;
    private Button addBillItemButton;
    private ListView lvBillItemList;
    private TextView tvBillSummary;

    private List<Item> itemList;

    @Override
    protected void addOkButtonClickOpt(View v) {
        billManager.create(location, payerId);
    }

    @Override
    protected void bindWidgetOnCreate() {
        TabHost tabs = (TabHost) this.findViewById(R.id.billTabHost);
        tabs.setup();

        TabHost.TabSpec basic = tabs.newTabSpec("Basic");
        basic.setContent(R.id.billBasicTabContent);
        basic.setIndicator(getString(R.string.bill_crud_basic_tab),
                getResources().getDrawable(
                        android.R.drawable.ic_menu_preferences));
        tabs.addTab(basic);

        TabHost.TabSpec itemList = tabs.newTabSpec("ItemList");
        itemList.setContent(R.id.billItemListTabContent);
        itemList.setIndicator(getString(R.string.bill_crud_item_list_tab),
                getResources().getDrawable(
                        android.R.drawable.ic_menu_recent_history));
        tabs.addTab(itemList);

        etLocation = (EditText) findViewById(R.id.locationEditText);
        spPayerId = (Spinner) findViewById(R.id.payerSpinner);
        spPayerId.setAdapter(DataList.getIdNameSimpleAdapter(this, userManager
                .getIdNameMapList()));
        billItemList = (LinearLayout) findViewById(R.id.billItemList);
        addBillItemButton = (Button) findViewById(R.id.addBillItemButton);
        lvBillItemList = (ListView) findViewById(R.id.dataListView);
        tvBillSummary = (TextView) findViewById(R.id.billSummaryTextView);
    }

    @Override
    protected void deleteBeanOkButtonClickOpt(DialogInterface dialog) {
        billManager.delete(id);
    }

    @Override
    protected int getContentView() {
        return R.layout.billcruddia;
    }

    @Override
    protected String getDeleteDataConfirmTip() {
        return this.getString(R.string.deleteBillConfirmTip)
                + billManager.getName(bill);
    }

    @Override
    protected boolean initializeValuesFromWidget() {
        location = etLocation.getText().toString();
        payerId = (Integer) ((Map<String, Object>) spPayerId.getSelectedItem())
                .get(BasicDataManager.ID);
        return true;
    }

    @Override
    protected void setValueFromIntentExtrasOnCreate() {
        billManager = Main.dataManager.billDataManager();
        userManager = Main.dataManager.userDataManager();
        itemManager = Main.dataManager.itemDataManager();
    }

    @Override
    protected void updateBeanWidgetInitialize() {
        bill = billManager.get(id);
        location = bill.getLocation();
        payerId = bill.getUser_id();

        etLocation.setText(location);
        setSpinnerSelector(spPayerId, payerId);

        setBillItemList();
    }

    private void setBillItemList() {
        billItemList.setVisibility(View.VISIBLE);
        lvBillItemList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view,
                    int position, long idView) {
                Map<String, Object> dataMap = (Map<String, Object>) adapter
                        .getItemAtPosition(position);
                Bundle bundle = new Bundle();
                Item item = itemManager.get((Integer) dataMap
                        .get(BasicDataManager.ID));
                bundle.putInt(BasicDataManager.ID, item.getId());
                bundle.putInt(BasicDataManager.BILL_ID, id);
                bundle
                        .putInt(BasicDataManager.PRODUCT_ID, item
                                .getProduct_id());
                bundle.putBoolean(DataList.IS_ADD, false);
                Intent intent = new Intent();
                intent.setClass(BillCRUDDialog.this,
                        ProductSearchActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        addBillItemButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent nextPageIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt(BasicDataManager.BILL_ID, id);
                bundle.putBoolean(DataList.IS_ADD, true);
                nextPageIntent.putExtras(bundle);
                nextPageIntent.setClass(BillCRUDDialog.this,
                        ProductSearchActivity.class);
                startActivity(nextPageIntent);
            }
        });

    }

    private void updateBillItemList(int billId) {
        itemList = itemManager.getByBillId(billId);
        lvBillItemList.setAdapter(DataList.getIdNameSimpleAdapter(this,
                itemManager.getIdNameMapList((List) itemList)));
        tvBillSummary.setText(billManager.getSummary(billId, itemManager,
                Main.dataManager.productDataManager()));
        // Utility.setListViewHeightBasedOnChildren(lvBillItemList);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            updateBillItemList(id);
        }
    }

    @Override
    protected void updateOkButtonClickOpt(View v) {
        billManager.update(id, location, payerId);
    }

}
